<template>
  <div class="prompt">
    <div class="addToolsSetof">
      <div class="item">
        <div class="lable">名称：</div>
        <el-input v-model="dataFrom.ptName" placeholder="请输入内容" disabled></el-input>
      </div>
      <div class="item">
        <div class="lable">描述：</div>
        <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 3 }" v-model="dataFrom.ptDesc" placeholder="请输入内容"></el-input>
      </div>
      <div class="item">
        <div class="lable">内容：</div>
        <el-input type="textarea" :autosize="{ minRows: 7, maxRows: 10 }" v-model="dataFrom.ptContent" placeholder="请输入内容"></el-input>
      </div>
    </div>
    <div class="btn">
      <el-button size="medium" type="primary" @click="submitFun">提交</el-button>
      <el-button size="medium" @click="$emit('closeDialog')">取消</el-button>
    </div>
  </div>
</template>
<script>
import { toolPromptList, toolPromptAdd, toolPromptUpdate, tkPromptUpdate, tkPromptList } from "@/api/tools.js"

export default {
  components: {},
  props: {
    tkName: {
      type: String,
      default: "",
    },
    tkId: {
      type: Number,
      default: 0,
    },
    toolName: {
      type: String,
      default: "",
    },
    toolId: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      dataFrom: {
        ptName: "",
        ptDesc: "",
        ptContent: "",
      },
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    getList() {
      if (this.tkId) {
        tkPromptList({ id: this.tkId })
          .then(result => {
            console.log(result)
            if (result.data) {
              this.dataFrom = {
                ptName: result.data.tkName,
                ptDesc: result.data.tkDesc,
                ptContent: result.data.tkPrompt,
              }
            }
          })
          .catch(err => {
            console.log(err)
          })
      } else {
        toolPromptList(this.toolId)
          .then(result => {
            console.log(result)
            // if (result.data.records.length) {
            //   this.dataFrom = result.data.records[0]
            // }
            if (result.data) {
              this.dataFrom = {
                ptName: result.data.resourceName,
                ptDesc: result.data.description,
                ptContent: result.data.toolPrompt,
              }
            }
          })
          .catch(err => {
            console.log(err)
          })
      }
    },
    submitFun() {
      if (!this.dataFrom.ptName) {
        this.$message.error("名称不能为空")
        return
      } else if (!this.dataFrom.ptDesc) {
        this.$message.error("描述不能为空")
        return
      } else if (!this.dataFrom.ptContent) {
        this.$message.error("内容不能为空")
        return
      }
      if (this.tkId) {
        this.submitTkFun()
        return
      }
      // if (this.dataFrom.id) {
      toolPromptUpdate({
        id: this.toolId,
        resourceName: this.dataFrom.ptName,
        toolPrompt: this.dataFrom.ptContent,
        description: this.dataFrom.ptDesc,
      })
        .then(result => {
          console.log(result)
          if (result.code == 200) {
            this.$message.success(result.msg)
            this.$emit("closeDialog")
          }
        })
        .catch(err => {
          console.log(err)
        })
      // } else {
      //   toolPromptAdd([
      //     {
      //       ptName: this.dataFrom.ptName,
      //       ptDesc: this.dataFrom.ptDesc,
      //       ptContent: this.dataFrom.ptContent,
      //       tkName: this.tkName,
      //       toolName: this.toolName || "",
      //     },
      //   ])
      //     .then(result => {
      //       console.log(result)
      //       if (result.code == 200) {
      //         this.$message.success(result.msg)
      //         this.$emit("closeDialog")
      //       }
      //     })
      //     .catch(err => {
      //       console.log(err)
      //     })
      // }
    },
    submitTkFun() {
      tkPromptUpdate({
        id: this.tkId,
        tkName: this.dataFrom.ptName,
        tkPrompt: this.dataFrom.ptContent,
        tkDesc: this.dataFrom.ptDesc,
      })
        .then(result => {
          console.log(result)
          if (result.code == 200) {
            this.$message.success(result.msg)
            this.$emit("closeDialog")
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
  },
}
</script>
<style lang="scss" scoped>
.prompt {
  height: 100%;
  .addToolsSetof {
    // display: flex;
    // flex-direction: column;
    // justify-content: space-evenly;
    height: calc(100% - 80px);
    overflow: auto;
    .item {
      display: flex;
      margin: 10px 20px 30px 20px;
      align-items: center;
      .lable {
        min-width: 80px;
        text-align: right;
      }
      .el-textarea,
      .el-input {
        width: 80%;
      }
    }
  }
  .btn {
    margin-top: 20px;
  }
}
</style>
